#

| Chapter 31 Creating Custom Commands in vi |

## 31.9 Good Stuff for Your .exrc File

[You probably won't want all of these keymaps and abbreviations. I always hate to re-map keys unless I really have to-I'd rather remember the real command instead of my special map that works only from my account. But not everyone agrees with me. And I've gotta admit that Tom has some pretty useful-looking customizations in this file. Wherever you see `^V` in this printout, that means that you type CTRL-v to protect the next key from interpretation by ex (31.6). In that case, the `^V` is not stored in the macro. If you see two of them (`^V^V`) that sequence will store an actual CTRL-v in the macro. The file on the CD-ROM is ready to use: it has the real control characters stored in it. Try it! One more note: if your fancy hacks to the .exrc file don't seem to be working, watch carefully for error messages just as vi starts, before it clears your screen. If you can't read them quickly enough, try the tricks in article 42.8. -JP]

    :m

    ^^

    \<..\>

    "nDdd`n@n `n

    :r!

    cat /dev/tty

    :%!

    less

    spell

    fmt

    expand

    sed

    ab
    "   INPUT MACROS that i always want active
    "
    map! ^Z ^[:stop^M
    "   so i can stop in input mode.  note that autowrite is set, so
    map! ^A ^[:stop!^M
    "   will stop me without writing.
    "
    map! ^K ^V^[O
    "   lets me do kind of a negative carriage return in input mode.
    map! ^V^[^B ^[bi
    "   non-destructive ^W
    map! ^V^[^F ^[Ea
    "   and its inverse
    "
    "   EMACS Style ARROW MACROS
    "
    map! ^B ^V^[i
    map! ^F ^V^[lli
    map! ^A ^V^[I
    map! ^E ^V^[A
    "
    "
    "   EXCHANGE MACROS - for exchanging things
    "
    map v xp
    "   exchange current char with next one in edit mode
    map V :m+1^M
    "   exchange current line with next one in edit mode
    map! ^P ^V^[hxpa
    "   exchange last typed char with penultimate one in insert mode
    map = ^^
    "   edit previously edited file
    "
    "   OTHER MACROS
    "
    map ^W :w^M
    "   write out the file
    "
    map * i^M^[
    "   split line
    "
    map ^A :stop!^M
    "   unconditional stop
    map Y y$
    "   so Y is analogous to C and D
    map ^R ddu
    "   single-line redraw
    map ^N :n +/^M
    "   go to next file in arg list, same position
    "   useful for "vi +/string file1 file2 file3"
    "
    "
    "   META MACROS, all begin with meta-key '\' ; more later in file
    "
    map ^V^I  \
    "   so can use both ^I [tab -JP ] and \ for meta-key
    "
    map \/ dePo/\<^V^[pA\>^V^["wdd@w
    "   find current word, uses w buffer
    "
    "
    "   INVERT WORD CASE - V is like W, v is like w.  3V is ok, only to EOL.
    "   uses both register n and mark n.
    map \v ywmno^[P:s/./\~/g^M0"nDdd`n@n
    "   abc -> ABC    ABC->abc
    map \V yWmno^[P:s/./\~/g^M0"nDdd`n@n
    "   abc.xyz -> ABC.XYZ    ABC.XYZ->abc.xyz
    "
    "
    "   EXECUTION MACROS - these two are for executing existing lines.
    "
    map \@ ^V^["mdd@m
    "   execute line as a straight vi command (buffer m, use @@ to repeat)
    map \! 0i:r!^V^["ndd@n
    "   execute line as :r! command (buffer n, use @@ to repeat)
    "
    map \t :r!cat /dev/tty^M
    "   read in stuff from X put buffer [in X window system -JP ]
    "
    "
    "   BLOCK MACROS - these help when dealing with C blocks
    "
    map! ^O ^V^V^V{^M^V^V^V} ^V^[O^T
    "   this will begin a block, leaving in insert mode
    map! ^] ^V^[/^V^V^V}^V^Ma
    "   and this will take you past its end, leaving in insert mode
    "
    "
    "
    "   LINT MACRO.  deletes all text from "lint output:" and below, (if any)
    "   replacing it with lint output in pretty block comment form.  could
    "   do sed work myself, but this is faster.
    "
    "   the map! is for subsequent map, not by people,
    "   though /^Lo would make sense.
    "   this is one of those famous time/space tradeoffs
    "
    map! ^Lo lint output
    "
    "   and now for the real work
    map \l Go^M/* ^Lo^M^[/^Lo^MdG:w^Mo/*** ^Lo^[<<
    :r!lint -u -lc %^V|sed 's/^/ *  /'^MGo***/^[N
    "   [NOTE: We split the macro above onto two lines for printing.
    "   Type it on one line in your .exrc file. -JP ]
    "
    "   indent this for me
    "
    map \i :%!indent -i4^M
    "
    "   COMMENTING MACROS - these are actually pretty amazing
    "
    "   from edit mode, this comments a line [for the C language -JP ]
    map ^X ^i/* ^[A */^[^
    "
    "   and this undoes it
    map ^Y :s/\/\* \([^*]*\) \*\//\1^[
    "
    "   this next one defeats vi's tail-recursion defeatism
    "   called by 2 maps following this one
    map! ^N ^V^[:unmap! ^V^V^M^[
    "
    "   while in insert mode, this will put you "inside" a comment
    map! ^X ^V^[:map! ^V^V^M ^V^V^[a^V^V^V^No^[a /*  */^[hhi
    "
    "   while in edit mode, this begins a block comment - ^N to escape
    map \c O/*^M *  ^M*/^[k:map! ^V^V^M ^V^V^M*  ^MA
    "
    "   and this is for adding more lines to a block comment - ^N to escape
    map \o :map! ^V^V^M ^V^V^M*  ^MA
    "
    "
    map _ i_^V^V^V^H^V^[ll
    "   this character will now be underlined when less'd, rn'd, etc.
    "
    "   SPELL MACROS
    "
    map \s :w^MGo^V^[:$r!spell %^M
    "   spell the file, placing errors at bottom, use \n to find
    map \n Gdd\/
    "   for find next spelling error
    "
    "
    "   FORMATTING MACROS
    "
    map \P :.,$!fmt -75^M
    "   format thru end of document
    map \p !}fmt -75^M
    "   format paragraph
    map \f 1G/--^Mj:.,$!fmt -75^M
    "   format message (assumes MH Mail "comp" format)
    map \e :%!expand -4^M
    "   expand tabs to 4 stops
    "
    map \r 1G/^-/^[:r!sed -e '1,/^$/d' -e 's/^./> &/' @ ^[/^-/^[j
    "   read in @, quoted (for MH replies, who link @ to current file)

    map \d :s/$/ $/^M$r 74^V|? ^V^Ms^M^[$xx0
    "   split line, like !!fmt but cheaper
    "
    "
    ab Jan January
    ab jan january
    ab Feb February
    ab feb february
    ab Sep September
    ab sep september
    ab Oct October
    ab oct october
    ab Nov November
    ab nov november
    ab Dec December
    ab dec december
    ab Xmas Christmas
    ab xmas christmas
    ab Mon Monday
    ab mon monday
    ab Tue Tuesday
    ab tue tuesday
    ab Wed Wednesday
    ab wed wednesday
    ab Thu Thursday
    ab thu thursday
    ab Fri Friday
    ab fri friday
    ab Sat Saturday
    ab Sun Sunday
    ab Int International
    ab info information

[Here's a macro by Greg Ubben that centers your current line:

>     map ^K 80I ^V^[$78hd0^D:s/  / /g^V^M$p

(`^K`, `^V`, `^[`, and `^M` are control characters, but `^D` is `^` and `D`.) This centers on an 80-column line. If any lines are close to 80 characters long now, the macro will work better with your wrapmargin set to 0. -JP ]

- TC

31.8 More Examples of Mapping Keys in vi |  | 31.10 Repeating a vi Keymap